﻿/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品版权所有。
// 命名空间: TongTongMall.MallWeb.Application.Users
// 类名：UserRedisManager
// 功能描述：N/A
// 
// 创建标识：Roc.Lee(李鹏鹏) 2017/8/26 9:38:38 TEC-ROCLEE Roc.Lee

// 修改标识：
// 修改描述：
// 
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Domain.Services;
using TongTongMall.Events;
using TongTongMall.MallWeb.Core.Events.EventDatas;
using TongTongMall.Oauth2._0.Models;
using TongTongMall.Redis.Cache;
using TongTongMall.Redis.Cache.Configuration;

namespace TongTongMall.MallWeb.Application.Users
{
    public partial class UserRedisManager : IDomainService
    {
        private IRedisCache _redisCache;

        /// <summary>
        /// 
        /// </summary>
        public IEventBus EventBus { get; set; } = NullEventBus.Instance;

        /// <summary>
        /// 
        /// </summary>
        public IRedisConfiguration RedisConfigurationManager { get; set; } = RedisConfiguration.Default;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_redisCache"></param>
        public UserRedisManager(IRedisCache _redisCache)
        {
            this._redisCache = _redisCache;
        }

        /// <summary>
        /// 检查用户信息是否存在
        /// {tenantname}:users:{username}:{tk}
        /// </summary>
        /// <param name="codeKey">{tenantname}:{username}:{tk}</param>
        /// <returns></returns>
        public virtual async Task<bool> CheckUserExits(params string[] codeKey)
        {
            return await _redisCache.ExistsAsync(GroupRedisKey(codeKey));
        }

        /// <summary>
        /// 获取用户信息
        ///  {tenantname}:{username}:{tk}
        /// </summary>
        /// <typeparam name="TType"></typeparam>
        /// <param name="codeKey">{tenantname}:{username}:{tk}</param>
        /// <returns></returns>
        public virtual async Task<TType> GetUserInfo<TType>(params string[] codeKey) where TType : OAuthRedisUser
        {
            var userDetail = await _redisCache.GetOrDefaultAsync(GroupRedisKey(codeKey));
            return (TType)userDetail ?? default(TType);
        }

        /// <summary>
        /// 设置用户信息
        /// {tenantname}:{username}:{tk}
        /// </summary>
        /// <typeparam name="TType"></typeparam>
        /// <param name="userData"></param>
        /// <param name="expireTime"></param>
        /// <param name="codeKey">{tenantname}:{username}:{tk}</param>
        /// <returns></returns>
        public virtual void SetUserInfo<TType>(TType userData, TimeSpan expireTime, params string[] codeKey) where TType : OAuthRedisUser
        {
            if (userData == null) throw new TongTongMallException("the userData is null！");
            EventBus.Trigger(typeof(RedisParameterEventData), this, new RedisParameterEventData { Keys = GroupRedisKey(codeKey), Data = userData, Expire = expireTime });
        }

        /// <summary>
        /// 组合redis的value
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        protected string GroupRedisKey(params string[] values)
        {
            return RedisConfigurationManager.GroupKeys(values);
        }
    }
}
